package cn.zs.supply.common.utils;


import java.io.File;
import java.io.IOException;

import org.apache.commons.lang3.StringUtils;
import org.im4java.core.ConvertCmd;
import org.im4java.core.IMOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ImageUtil {

    private final static Logger log=LoggerFactory.getLogger(ImageUtil.class);

    private static final String THUMBNAIL = "tmbf_";
    private static final String allowExtention = ".avi,.mov,.mp4,.wmv,.3gp,.rmvb,.mkv,.m4v";
    private static int commitSize = 500;
    private static Long TIF_JPEG_SIZE = 1048576l;

    private static final String FILE_PATH = ConfigProperties.getProperty("fs.storage.local.root");


    /**
     * 按像素生成图片缩略图(宽166高148)
     * @param srcPath 源文件磁盘路径  如：/data/images
     * @param filePath 文件所在路径   如：/2017/10/01/orderNo/test.jpg
     * @return 返回磁盘文件路径
     */
    public static String thumbnailFile(String srcPath, String filePath) {
        String newPath = StringUtils.EMPTY;
        String fileName = StringUtils.EMPTY;
        try {
            String extention = filePath.substring(filePath.lastIndexOf(".")).toLowerCase();
            if (!allowExtention.contains(extention)) {
                File file = new File(srcPath + filePath);
                String path = filePath.substring(0, filePath.lastIndexOf("/") + 1);
                fileName = path + THUMBNAIL + file.getName();
                newPath = srcPath + fileName;
                cutImage(166, 148, file.getAbsolutePath(), newPath, 1, null);
            }
            return fileName;
        } catch (IOException e) {
            log.error("文件裁剪出错, srcPath:[{}], filePath:[{}]",  srcPath, filePath);
            return StringUtils.EMPTY;
        }
    }


    /**
     * 根据尺寸缩放图片
     *
     * @param width
     *            缩放后的图片宽度
     * @param height
     *            缩放后的图片高度
     * @param srcPath
     *            源图片路径
     * @param newPath
     *            缩放后图片的路径
     * @param type
     *            1为像素，2为百分比处理，如（像素大小：1024x1024,百分比：50%x50%）
     * @param quality
     *            压缩质量(默认空)
     */
    private static String cutImage(int width, int height, String srcPath,
                                   String newPath, int type, String quality) throws IOException {
        IMOperation op = new IMOperation();
        ConvertCmd cmd = new ConvertCmd(true);
        //cmd.setSearchPath("D:\\Program Files (x86)\\GraphicsMagick-1.3.23-Q16");
        op.addImage();
        String raw = "";
        if (type == 1) {
            // 按像素
            raw = width + "x" + height + "^";
        } else {
            // 按像素百分比
            raw = width + "%x" + height + "%";
        }
        op.addRawArgs("-sample", raw);
        op.addRawArgs("+profile", "*");// 去除Exif信息，可减小文件大小
        if ((quality != null && !quality.equals(""))) {
            op.addRawArgs("-quality", quality);
        }
        op.addImage();
        try {
            cmd.run(op, srcPath, newPath);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return newPath;
    }

}
